<?php
/**
 * class for autoloading all other classes
 *
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
final class autoload {
    protected static $instance = NULL;
    protected static $directories = Array(
      '../library/',
      '../application/',
      '../application/models/',
      '../application/controllers/',
      '../application/controllers/message_elements/',
      '../application/views/',
    );

    protected function __construct() {
        spl_autoload_register(Array($this, 'load'));
    }

    /**
     * method to initialize the autoloader
     *
     * @static
     * @return autoload the autoload class instance
     */
    public static function init() {
        if (self::$instance == NULL) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    protected function load($classname) {
        $root_dir = rtrim($_SERVER['DOCUMENT_ROOT'], '/');

        foreach (self::$directories as $dir) {
            $class_file = $root_dir.'/'.$dir.$classname.'.class.php';

            if (is_file($class_file)) {
                require_once($class_file);
                return true;
            }

            $class_file = $root_dir.'/'.$dir.$classname.'.php';
            if (is_file($class_file)) {
                require_once($class_file);
                return true;
            }
        }

        throw new Exception("Class '$classname' has not been found!");
    }
}
?>